package first.endtoend;

import java.util.Map;

import org.mbds.wolf.seql.ISeqlCallBack;
import org.mbds.wolf.seql.ISeqlController;
import org.mbds.wolf.seql.SeqlController;
import org.mbds.wolf.seql.exceptions.APDUError;

import android.app.Application;
import android.widget.Toast;

import com.api.sqlitehelper.Utils;

import first.endtoend.dal.EndToEndSqliteHelper;

public class MyApplication extends Application implements ISeqlCallBack {
	 private ISeqlController ctrl = null;
//	 public boolean areSEAndAppletPresents = false;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		init();
	}

	protected void init() {
		boolean ok = false;
		try {
			SeqlController.CONFIG os = SeqlController.CONFIG.ANDROID_HCE;
			if (android.os.Build.VERSION.SDK_INT<=19)
				os = SeqlController.CONFIG.ANDROID_OMAPI;
			ctrl =SeqlController.getController(getApplicationContext(), os, this, null, EndToEndHCEService.class.getName());
			ctrl.initService();
			ok = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!ok) {
			Toast.makeText(this, "SE-QL controller could not be instantiated, application will finish!", Toast.LENGTH_LONG).show();
			quitApp();
		}
		Utils.helper = new EndToEndSqliteHelper(this, null);
	}
	
	public void quitApp() {
		System.exit(0);
	}

	public ISeqlController getSeqlController() {
		return ctrl;

	}
	

	@Override
	public void onNotConnected() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConnected() {
		System.out.println("Connected to SE");
//		ctrl.sayHello("pds_applet",Constant.SAY_HELLO_CODE);
	}

	@Override
	public void onNoSecureElement() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNoReader() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNoApplet() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAPDUError(APDUError error) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onIOException(String message) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPINRequired() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onBadRequest(String message) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResponse(Map<String, Object> results, int code) {
		// TODO Auto-generated method stub
//		switch (code) {
//		case Constant.SAY_HELLO_CODE:
//			areSEAndAppletPresents = true;
//			break;
//		}
	}

	@Override
	public void onConfigError() {
		// TODO Auto-generated method stub
		
	}

}
